package com.ocean.aws.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;

public final class CompressionUtils {

	public static byte[] gzip(byte[] data) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try (GZIPOutputStream gos = new GZIPOutputStream(baos)) {
			gos.write(data);
		}
		return baos.toByteArray();
	}

	public static byte[] ungzip(byte[] data) throws IOException {
		try (GZIPInputStream gis = new GZIPInputStream(new java.io.ByteArrayInputStream(data))) {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buf = new byte[8192];
			int r;
			while ((r = gis.read(buf)) != -1) {
				baos.write(buf, 0, r);
			}
			return baos.toByteArray();
		}
	}
}


